<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="s" uri="http://stripes.sourceforge.net/stripes.tld" %>

<s:layout-render name="/layout_template.jsp" nadpis="businessTrip.mainHeading">
    <s:layout-component name="hlavicka">
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
        <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
        <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
        <script>
            $(function() {
                $( "#datepicker" ).datepicker({ dateFormat: "<f:message key="dateFormat"/>" });
            });
            function show(id){
                document.getElementById(id).style.display = 'block';
            }
        </script>
        <script>
            $(function() {
                $( "#datepicker2" ).datepicker({ dateFormat: "<f:message key="dateFormat"/>" });
            });
            function show(id){
                document.getElementById(id).style.display = 'block';
            }
        </script>
    </s:layout-component>
    <s:layout-component name="telo">
        <s:useActionBean beanclass="cz.muni.fi.pa165.carparkweb.actionbeans.EmployeeActionBean" var="actionBean"/>
        <h2><f:message key="businessTrip.tripsForEmployeeHeading"/> <c:out value="${actionBean.employee.name}"/></h2> 
        <table>
            <tbody>
                <tr>
                    <s:form beanclass="cz.muni.fi.pa165.carparkweb.actionbeans.EmployeeActionBean" >
                        <td>  
                            <f:message key="fromDate"/>
                            <s:text id="datepicker" class="input" name="fromDate" size="15" tabindex="1"/>
                        </td>
                        <td>  
                            <f:message key="toDate"/>
                            <s:text id="datepicker2" class="input" name="toDate" size="15" tabindex="1"/>
                        </td>
                        <td>
                            <s:submit name="getTripsByDates"><s:param name="employee.id" value="${actionBean.employee.id}"/><f:message key="inInterval"/></s:submit>
                            </td>        
                    </s:form>

                    <s:form beanclass="cz.muni.fi.pa165.carparkweb.actionbeans.EmployeeActionBean" >
                        <td>
                            <s:submit name="listTripsForEmployee"><s:param name="employee.id" value="${actionBean.employee.id}"/><f:message key="allTrips"/></s:submit>
                            </td>     
                    </s:form>
                </tr>
                <tr>
                    <td/>
                </tr>
            </tbody>
        </table>
        <table id="basicview">
            <thead>
                <tr>
                    <th>ID</th>
                    <th><f:message key="businessTrip.vehicle"/></th>
                    <th><f:message key="businessTrip.employee"/></th>
                    <th><f:message key="businessTrip.date"/></th>
                    <th><f:message key="businessTrip.driven"/></th> 
                    <th/>
                </tr>
            </thead>            
            <tbody>
                <c:set var="sum" value="0"/>
                <c:forEach items="${actionBean.trips}" var="trip">
                    <c:if test="${trip.employee.id == actionBean.employee.id}">
                        <tr>                 
                            <td>${trip.id}</td>
                            <td><c:out value="${trip.vehicle.carProducer} ${trip.vehicle.carModel} - ${trip.vehicle.stateLicencePlate}"/></td>
                            <td><c:out value="${trip.employee.name}"/></td>                    
                            <f:message key="dateFormatForm" var="tripDate" />
                            <f:formatDate value="${trip.dateOfTrip}" pattern="${tripDate}" var="tripdatevar" />
                            <td><c:out value="${tripdatevar}" /></td>
                            <td><c:out value="${trip.numberOfDrivenKms}"/></td>
                            <c:set var="num" value="${trip.numberOfDrivenKms}"/>
                            <c:set var="sum" value="${sum + num}" />
                            <c:choose>
                                <c:when test="${trip.numberOfDrivenKms == 0}" >
                                    <td><s:link class="button" beanclass="cz.muni.fi.pa165.carparkweb.actionbeans.EmployeeActionBean" event="close">
                                            <s:param name="employee.id" value="${actionBean.employee.id}"/>
                                            <s:param name="businessTrip.id" value="${trip.id}"/><f:message key="close"/>
                                        </s:link> </td>
                                    </c:when>
                                    <c:otherwise>
                                    <td/>
                                </c:otherwise>
                            </c:choose>
                        </tr>
                    </c:if>
                </c:forEach>
            </tbody> 
            <tfoot> 
                <tr> 
                    <td colspan="5"><f:message key="sumDriven"/> km</td> 
                    <td><c:out value="${sum}"/></td>
                </tr> 
            </tfoot>
        </table>
        <br>
        <s:link class="button" beanclass="cz.muni.fi.pa165.carparkweb.actionbeans.EmployeeActionBean" event="addTrip"><s:param name="employee.id" value="${actionBean.employee.id}"/><f:message key="createNew"/></s:link>
        <s:link class="button" beanclass="cz.muni.fi.pa165.carparkweb.actionbeans.EmployeeActionBean" event="storno"><f:message key="back"/></s:link>
    </s:layout-component>
</s:layout-render>